﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SurvivalMan
{

    
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private SpriteFont poäng, ammo, eS, tid, start;
        private Texture2D playerL, playerR, enimieL, enimieR, background, arrowL, shot;
        private Player p1;
        private Enimie e1;
        private Background b1;
        private Collision c1;
        private Arrow a1;

        private Rectangle playerRectangle, enimieRectangle, arrowRectangle;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            poäng = Content.Load<SpriteFont>("poäng");
            ammo = Content.Load<SpriteFont>("ammo");
            eS = Content.Load<SpriteFont>("eS");
            tid = Content.Load<SpriteFont>("tid");
            start = Content.Load<SpriteFont>("start");
            background = Content.Load<Texture2D>("bakrund");          
            shot = Content.Load<Texture2D>("shot");
            playerL = Content.Load<Texture2D>("playerL");
            playerR = Content.Load<Texture2D>("playerR");            
            enimieL = Content.Load<Texture2D>("enimieL");
            enimieR = Content.Load<Texture2D>("enimieR");
           

           // arrowL = Content.Load<Texture2D>("");
            
            a1 = new Arrow(shot);
            
            c1 = new Collision();
            p1 = new Player(350, 390, playerL, playerR, a1, poäng, ammo, tid, start);
            e1 = new Enimie(0,0,1, enimieL, enimieR, eS);
            b1 = new Background(background);
            p1.playerTextureData = new Color[p1.playerLeft.Width * p1.playerLeft.Height];
            p1.playerLeft.GetData(p1.playerTextureData);
            e1.enimieTextureData = new Color[e1.enimieL.Width * e1.enimieL.Height];
            e1.enimieL.GetData(e1.enimieTextureData);
            a1.arrowTextureData = new Color[a1.arrow.Width * a1.arrow.Height];
            a1.arrow.GetData(a1.arrowTextureData);
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            p1.move(gameTime);
            e1.move(p1.getX(), p1.getY(), p1.getGS());

            playerRectangle = new Rectangle(p1.getX(), p1.getY(), p1.playerLeft.Width, p1.playerRight.Height);
            enimieRectangle = new Rectangle(e1.getX(), e1.getY(), e1.enimieL.Width, e1.enimieR.Height);
            arrowRectangle = new Rectangle((int)a1.position.X, (int)a1.position.Y, a1.arrow.Width, a1.arrow.Height);

            if (c1.kollision(playerRectangle, p1.playerTextureData, enimieRectangle, e1.enimieTextureData))
            {
                e1.setX(e1.randomSpawn(e1.getX()));
                e1.setY(50);
                p1.prevpoints = p1.points;
                p1.prevtime = p1.tiden;
                p1.points = 0;
                e1.speed = 1;
                p1.gameState = 0;
                //p1.tiden = 0;
                
            }

            if (c1.kollision(arrowRectangle, a1.arrowTextureData, enimieRectangle, e1.enimieTextureData))
            {
                e1.setX(e1.randomSpawn(e1.getX()));
                e1.setY(50);
                p1.points += 1;

                if (p1.points % 5 == 0)
                    e1.speed += 1;
            }


            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();

            b1.draw(spriteBatch);
            p1.draw(spriteBatch);
            e1.draw(spriteBatch, p1.getGS());

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
